package poswar.contribute.mbean;

import java.util.TreeSet;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;

import org.primefaces.event.SelectEvent;
import org.primefaces.model.tagcloud.TagCloudItem;

import poswar.contribute.dao.BoardDao;

@ManagedBean
@SessionScoped
public class IdeaBean {
	private String idea =  "";

	@ManagedProperty("#{cloudBean}")
	private CloudBean cloudBean;

	@ManagedProperty("#{userBean}")
	private UserBean userBean;
	
	public String launchIdea() {
		removeDuplicates();
		idea = cloudBean.setIdea(userBean.getEmail(), idea);
		
		return null;
	}

	public void onSelect(SelectEvent event) {
		TagCloudItem item = (TagCloudItem) event.getObject();
		idea = item.getLabel() + "\n" + idea;
		removeDuplicates();
		
		idea = cloudBean.setIdea(userBean.getEmail(), idea);
	}
	
	public void removeDuplicates() {
		TreeSet<String> itens = new TreeSet<String>();
		
		String ideas[] = idea.split("\\r?\\n");
		String output = "";
		
		for (String s: ideas) {
			if (!itens.contains(s)) {
				itens.add(s);
				
				output += s + "\n";
			}
		}
		
		idea = output;
	}	

	public String getIdea() {
		return idea;
	}

	public void setIdea(String idea) {
		this.idea = idea;
	}

	public void setCloudBean(CloudBean cloudBean) {
		this.cloudBean = cloudBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}
	
	
}
